<template>
  <div class="equipment-detail container">
    <!-- 面包屑 -->
    <div class="bread-crumb container">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>{{ $t("yunLianUsedMachine") }}</el-breadcrumb-item>
        <template v-if="locale == 'zh-CN'">
          <el-breadcrumb-item :to="{}" @click.native="handleProducts(1)">二手{{ equipmentDetail.typeName }}</el-breadcrumb-item>
          <el-breadcrumb-item :to="{}" @click.native="handleProducts(2)"
            >{{ equipmentDetail.provinceName }}二手{{ equipmentDetail.brandName + equipmentDetail.typeName }}</el-breadcrumb-item
          >
          <el-breadcrumb-item>二手{{ equipmentDetail.title }}</el-breadcrumb-item>
        </template>
        <template v-else>
          <el-breadcrumb-item :to="{}" @click.native="handleProducts(1)">used {{ equipmentDetail.typeName }}</el-breadcrumb-item>
          <el-breadcrumb-item>used {{ equipmentDetail.title }}</el-breadcrumb-item>
        </template>
      </el-breadcrumb>
    </div>
    <!-- 设备title、设备ID、所在省份、所在市区、围观次数、备注 -->
    <div class="header-view">
      <el-image class="info-img" fit="cover" :src="equipmentUrl" :preview-src-list="[equipmentUrl]" lazy :alt="equipmentDetail.title"></el-image>
      <div class="info-right">
        <div class="info-right-top">
          <div class="info-title">{{ equipmentDetail.title }}</div>
          <div class="info-tag" v-if="locale == 'zh-CN'">
            <p v-for="item in equipmentDetail.targerList" :key="item">{{ item }}</p>
          </div>
          <div class="info-use">
            <div class="use-item">
              <p>{{ $t("equipmentId") }}</p>
              <span>{{ equipmentDetail.id }}</span>
            </div>
            <div class="use-item" v-if="locale == 'zh-CN'">
              <p>所在市区</p>
              <span>{{ equipmentDetail.cityName }}</span>
            </div>
            <div class="use-item" v-if="locale == 'zh-CN'">
              <p>所在省份</p>
              <span>{{ equipmentDetail.provinceName }}</span>
            </div>
            <div class="use-item">
              <p>{{ $t("observationFrequency") }}</p>
              <span>{{ equipmentDetail.pv }}</span>
            </div>
          </div>
        </div>
        <div class="info-care" v-if="locale == 'zh-CN'">备注：{{ equipmentDetail.remark || "暂无备注说明" }}</div>
        <div class="info-share" v-if="locale == 'en-US'">
          <!-- <a :href="['http://www.facebook.com/sharer.php?u=' + hostUrl + routeUrl]" title="facebook" target="_blank">
            <img src="https://yunlian-oss-product.oss-cn-hangzhou.aliyuncs.com/website/facebook.png" />
          </a> -->
          <!-- <a :href="['http://twitter.com/share?text=' + equipmentDetail.title + '&url=' + hostUrl + routeUrl]" title="twitter" target="_blank">
            <img src="https://yunlian-oss-product.oss-cn-hangzhou.aliyuncs.com/website/twitter.png" />
          </a>
          <a :href="['https://www.linkedin.com/shareArticle?mini=true&url=' + hostUrl + routeUrl]" title="linkedin" target="_blank">
            <img src="https://yunlian-oss-product.oss-cn-hangzhou.aliyuncs.com/website/linkedin.png" />
          </a> -->
          <img src="https://yunlian-oss-product.oss-cn-hangzhou.aliyuncs.com/website/facebook.png" alt="facebook" @click="mediaShare('facebook')" />
          <img src="https://yunlian-oss-product.oss-cn-hangzhou.aliyuncs.com/website/twitter.png" alt="twitter" @click="mediaShare('twitter')" />
          <img src="https://yunlian-oss-product.oss-cn-hangzhou.aliyuncs.com/website/linkedin.png" alt="linkedin" @click="mediaShare('linkedin')" />
        </div>
      </div>
    </div>
    <!-- 基本信息 -->
    <div class="base-view">
      <div class="view-title">{{ $t("basicInformation") }}</div>
      <div class="archives-list">
        <div class="list-item" v-for="item in baseInfo.attrs" :key="item.title" v-show="item.subType != 20203">
          <p>{{ item.title }}</p>
          <span>{{ item.tempValue ? (item.tempValue == "不详" && locale == "en-US" ? "unknown" : item.tempValue) : "--" }}</span>
        </div>
      </div>
    </div>
    <!-- 设备图片 -->
    <div class="pic-video-view" v-for="item in photoList" :key="item.cardTitle">
      <div class="view-title">{{ item.cardTitle }}</div>
      <div class="photo-list">
        <div class="photo-item" v-for="(itemNew, index) in item.attrs" :key="index">
          <el-image
            class="img-url"
            fit="cover"
            :src="itemNew.map.thumbUrl"
            :preview-src-list="[itemNew.map.url]"
            lazy
            :alt="`${equipmentDetail.title}-${itemNew.title}`"
          />
          <p class="photo-info">{{ itemNew.title }}</p>
        </div>
      </div>
    </div>
    <!-- 设备视频 -->
    <div class="pic-video-view" v-if="videoList.length">
      <div class="view-title">{{ $t("deviceVideo") }}</div>
      <div class="photo-list">
        <div class="photo-item" v-for="(item, index) in videoList" :key="index">
          <client-only>
            <vueMiniPlayer ref="vueMiniPlayer" :video="item" :mutex="true" @fullscreen="handleFullscreen" />
          </client-only>
        </div>
      </div>
    </div>
    <!-- 设备推荐 -->
    <div class="view-title" style="margin-top: 30px" v-if="recommendList.length">{{ $t("recommendedGoodCars") }}</div>
    <section class="recommend-list" v-if="recommendList.length">
      <a
        class="equipment-item"
        v-for="item in recommendList"
        :key="item.crmEquipmentId"
        :href="`/equipment/${item.typeNameEn}-${item.crmEquipmentId}`"
        :title="`${item.title} | ${item.subHeading}`"
      >
        <el-image class="info-img" fit="cover" :src="item.thumbnail" lazy :alt="item.title"></el-image>
        <div class="info-bottom">
          <div class="info-title">{{ item.title }}</div>
          <div class="info-year">{{ item.subHeading }}</div>
          <div class="info-tag" v-if="locale == 'zh-CN'">
            <p v-for="val in item.targerList" :key="val">{{ val }}</p>
          </div>
          <div style="height: 18px" v-else></div>
          <div class="info-price">
            <template v-if="item.crmEquipmentFixedPriceId && item.offlineBargaining != 1">
              <p v-if="locale == 'en-US'">CNY{{ item.startingPriceNotag }}</p>
              <p v-else>{{ item.startingPrice / 1000000 }}万</p>
            </template>
            <p v-else>{{ $t("priceNegotiable") }}</p>
          </div>
        </div>
      </a>
    </section>
    <!-- 侧边栏服务 -->
    <Dangling v-if="locale == 'zh-CN'" />
    <!-- WhatsApp -->
    <!-- <WhatsApp v-if="locale == 'en-US'" /> -->
    <!-- 在线客服 -->
    <Connect />
  </div>
</template>

<script>
import { mapState } from "vuex"
import BigNumber from "bignumber.js"
export default {
  name: "EquipmentDetail",
  layout: "Layout",
  data() {
    return {
      hostUrl: "",
      routeUrl: ""
    }
  },
  head() {
    return {
      title: `【${this.equipmentDetail.title}】-${this.equipmentDetail.cityName}${this.equipmentDetail.title}${this.$i18n.t("detailHeadTitle")}`,
      meta: [
        {
          hid: "keywords",
          name: "keywords",
          content: `${this.equipmentDetail.title}，${this.equipmentDetail.title}${this.$i18n.t("haeaKeySell")}，${
            this.equipmentDetail.title
          }${this.$i18n.t("haeaKeyPrice")}`
        },
        {
          hid: "description",
          name: "description",
          content: `${this.$i18n.t("haeaDecProvide")}${this.equipmentDetail.cityName}${this.equipmentDetail.title}${this.$i18n.t("haeaDecTransfer")}${
            this.equipmentDetail.workingHours > 0 ? this.equipmentDetail.workingHours : this.$i18n.t("haeaDecUnknown")
          }${this.$i18n.t("haeaDecCondition")}${this.equipmentDetail.title}${this.$i18n.t("haeaDecChoose")}`
        },
        {
          property: "og:title",
          content: `${this.equipmentDetail.title}`
        },
        {
          property: "og:description",
          content: `${this.$i18n.t("haeaDecProvide")}${this.equipmentDetail.cityName}${this.equipmentDetail.title}${this.$i18n.t("haeaDecTransfer")}${
            this.equipmentDetail.workingHours > 0 ? this.equipmentDetail.workingHours : this.$i18n.t("haeaDecUnknown")
          }${this.$i18n.t("haeaDecCondition")}${this.equipmentDetail.title}${this.$i18n.t("haeaDecChoose")}`
        },
        {
          property: "og:url",
          content: `${this.hostUrl}${this.routeUrl}`
        },
        {
          property: "og:image",
          content: `${this.equipmentUrl}`
        }
      ]
    }
  },
  async asyncData({ app, params }) {
    let paramsRes = params.detailParams.split("-")
    console.log("paramsRes", paramsRes)
    let id = paramsRes[1] //设备ID
    let equipmentDetail = {} //设备详情
    let equipmentUrl = "" //设备url
    let baseInfo = {} //设备基本信息
    let photoList = [] //设备照片
    let videoList = [] //设备视频
    let recommendList = [] //好车推荐
    try {
      let detailRes = await app.$api.equipmentDetail({ id })
      let detail = detailRes.data
      detail["targerList"] = app.$optionsData.parseLabel(detail)
      equipmentDetail = detail
      let cardList = detail.equipmentDetailsConfig.cardList
      baseInfo = cardList.filter((v) => v.type == 202)[0]
      photoList = cardList.filter((v) => v.type == 204)
      equipmentUrl = photoList.length ? photoList[0].attrs[0].map.url : ""
      let videoInfo = cardList.filter((v) => v.type == 205)[0]
      videoList = []
      videoInfo &&
        videoInfo.attrs.map((item) => {
          let b = {
            url: item.map.originVideo,
            cover: item.map.videoThumbUrl,
            muted: false,
            loop: false,
            preload: "auto",
            poster: "",
            autoplay: false,
            playsinline: false,
            logo: "SANY",
            format: item.format,
            title: item.title
          }
          videoList.push(b)
        })
      baseInfo.attrs.map((item) => {
        if (item.subType === 20201) {
          if (Object.values(item.map)[0] != "-1" && Object.values(item.map)[0] != "0" && Object.values(item.map)[0]) {
            item["tempValue"] = Object.values(item.map)[0] + item.placeholder.split("{0}")[1]
          } else {
            if (Object.values(item.map)[0] == null || Object.values(item.map)[0] == "") {
              item["tempValue"] = ""
            } else {
              item["tempValue"] = "不详"
            }
          }
        }
        if (item.subType === 20202) {
          let key = Object.values(item.map)[0]
          item.options.forEach((one) => {
            if (one.value === key) {
              item["tempValue"] = one.name
            }
          })
        }
      })

      let recommendRes = await app.$api.equipmentRecommend({ equipmentId: id, limit: 8 })
      let list = recommendRes.data
      list.map((item) => {
        item.startingPriceNotag = new BigNumber(item.startingPrice).dividedBy(100).toFormat()
        let targerList = app.$optionsData.parseLabel(item)
        targerList = targerList.splice(0, 2)
        item["targerList"] = targerList
        return item
      })
      recommendList = list
    } catch (error) {
      console.log("error", error)
    }
    return {
      equipmentDetail,
      equipmentUrl,
      baseInfo,
      photoList,
      videoList,
      recommendList
    }
  },
  props: {},
  computed: { ...mapState(["locale"]) },
  watch: {},
  async mounted() {
    this.$cookies.set("msgInfo", true) //缓存检索存储值
    this.currentUrl()
    this.getPoint()
  },
  methods: {
    currentUrl() {
      setTimeout(() => {
        if (process.client) {
          this.hostUrl = `${window.location.host}`
          this.routeUrl = `${this.$route.fullPath}`
        }
      }, 1000)
    },
    mediaShare(type) {
      let linkUrl = ""
      let url = encodeURIComponent(`${window.location.host}${this.$route.fullPath}`)
      if (type == "facebook") {
        linkUrl = `http://www.facebook.com/sharer.php?u=${url}&t=${this.equipmentDetail.title}`
        _hmt.push(["_trackEvent", "button", "click", `分享设备到facebook，设备ID-${this.equipmentDetail.id}`])
      } else if (type == "twitter") {
        linkUrl = `https://twitter.com/intent/tweet?url=${url}&title=${this.equipmentDetail.title}`
        _hmt.push(["_trackEvent", "button", "click", `分享设备到twitter，设备ID-${this.equipmentDetail.id}`])
      } else if (type == "linkedin") {
        linkUrl = `https://www.linkedin.com/shareArticle?mini=true&url=${url}&title=${this.equipmentDetail.title}`
        _hmt.push(["_trackEvent", "button", "click", `分享设备到linkedin，设备ID-${this.equipmentDetail.id}`])
      }
      window.open(linkUrl, "_blank")
    },
    handleFullscreen(data) {
      console.log("data", data)
    },
    handleProducts(num) {
      this.$cookies.remove("msgInfo") //清除检索存储值
      this.$cookies.remove("searchCondition")
      let equipmentTypePy = this.$optionsData.equipmentTypePy[this.equipmentDetail.typeId]
      if (num == 1) {
        this.$router.push(`/used-machine/${equipmentTypePy}-${this.equipmentDetail.typeId}`)
      } else {
        this.$router.push(`/used-machine/${equipmentTypePy}-${this.equipmentDetail.typeId}-${this.equipmentDetail.provinceCode}`)
      }
    },
    async getPoint() {
      let params = await this.$optionPoint.sendUrl(window, navigator, document)
      this.$api.burySave({ ...params }).then((res) => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.equipment-detail {
  display: flex;
  flex-direction: column;
  .bread-crumb {
    margin-top: 30px;
  }
  .header-view {
    display: flex;
    justify-content: space-between;
    margin-top: 30px;
    .info-img {
      width: 610px;
      height: 400px;
    }
    .info-right {
      width: 550px;
      height: 400px;
      display: flex;
      flex-direction: column;
      .info-right-top {
        .info-title {
          font-size: 22px;
          font-weight: 500;
          color: #333;
          line-height: 24px;
        }
        .info-tag {
          display: flex;
          align-items: center;
          margin-top: 10px;
          p {
            height: 18px;
            line-height: 18px;
            padding: 2px 5px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 400;
            color: #fb463f;
            background: rgba(251, 70, 63, 0.08);
            margin-right: 5px;
          }
        }
        .info-use {
          width: 550px;
          height: 96px;
          background: #f6f7f9;
          padding: 20px;
          box-sizing: border-box;
          display: flex;
          flex-wrap: wrap;
          margin-top: 20px;
          .use-item {
            width: 50%;
            display: flex;
            align-items: center;
            p {
              min-width: 74px;
              font-size: 16px;
              font-weight: 500;
              color: #999;
              line-height: 18px;
            }
            span {
              font-size: 16px;
              font-weight: 500;
              color: #333;
              line-height: 18px;
              margin-left: 10px;
            }
          }
        }
      }
      .info-care {
        width: 550px;
        height: 208px;
        background: #f6f7f9;
        padding: 17px 20px;
        box-sizing: border-box;
        font-size: 16px;
        font-weight: 500;
        color: #333;
        line-height: 24px;
        margin-top: 20px;
      }
      .info-share {
        display: flex;
        align-items: center;
        justify-content: right;
        margin-top: 20px;
        padding-right: 10px;
        box-sizing: border-box;
        img {
          width: 40px;
          height: 40px;
          cursor: pointer;
        }
      }
    }
  }
  .view-title {
    font-size: 22px;
    font-weight: 500;
    color: #333;
    line-height: 24px;
    margin-bottom: 30px;
  }
  .base-view {
    margin-top: 30px;
    margin-bottom: 10px;
    .archives-list {
      display: flex;
      flex-wrap: wrap;
      .list-item {
        width: 33.33%;
        display: flex;
        align-items: center;
        margin-bottom: 20px;
        p {
          min-width: 120px;
          height: 20px;
          font-size: 18px;
          font-weight: 500;
          color: #999;
          line-height: 20px;
          flex-shrink: 0;
        }
        span {
          font-size: 18px;
          font-weight: 500;
          color: #333;
          line-height: 20px;
          margin-left: 10px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
  .pic-video-view {
    .photo-list {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      .photo-item {
        width: 290px;
        height: 190px;
        margin-bottom: 30px;
        position: relative;
        .img-url {
          width: 100%;
          height: 100%;
        }
        .photo-info {
          width: 100%;
          height: 30px;
          line-height: 30px;
          text-align: center;
          background: rgba(0, 0, 0, 0.5);
          font-size: 16px;
          font-weight: 400;
          color: #fff;
          position: absolute;
          left: 0;
          bottom: 0;
        }
        &:last-child:nth-child(4n - 1) {
          margin-right: calc(24% + 4% / 3);
        }
        &:last-child:nth-child(4n - 2) {
          margin-right: calc(48% + 8% / 3);
        }
      }
    }
  }
  .recommend-list {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    height: 100%;
    .equipment-item {
      width: 290px;
      height: 100%;
      margin-bottom: 30px;
      .info-img {
        width: 290px;
        height: 184px;
        display: block;
      }
      .info-bottom {
        width: 100%;
        height: 116px;
        background: #f6f7f9;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        padding: 10px;
        box-sizing: border-box;
        .info-title {
          font-family: PingFangSC, PingFang SC;
          font-weight: 500;
          font-size: 18px;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .info-year {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #999;
          line-height: 16px;
        }
        .info-tag {
          display: flex;
          align-items: center;
          p {
            height: 18px;
            line-height: 18px;
            padding: 2px 5px;
            border-radius: 4px;
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 12px;
            color: #fb463f;
            background: rgba(251, 70, 63, 0.08);
            margin-right: 5px;
          }
        }
        .info-price {
          display: flex;
          align-items: flex-end;
          flex-direction: row;
          justify-content: space-between;
          p {
            font-size: 16px;
            font-weight: 700;
            color: #fb463f;
          }
        }
      }
      &:last-child:nth-child(4n - 1) {
        margin-right: calc(24% + 4% / 3);
      }
      &:last-child:nth-child(4n - 2) {
        margin-right: calc(48% + 8% / 3);
      }
      &:hover {
        box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.2);
        .info-bottom {
          background: #fff;
        }
      }
    }
  }
}
::v-deep .vm-player {
  min-height: 0;
  ._play-btn {
    z-index: 1;
  }
  ._logo {
    z-index: 0;
  }
  .qun-base-controls {
    z-index: 0;
  }
}
</style>
